package com.s7175.kor.data;

import java.io.File;

import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.db4o.config.Configuration;
import com.db4o.config.ObjectClass;
import com.db4o.ext.ExtObjectContainer;
import com.s7175.kor.model.ArchitectureView;
import com.s7175.kor.model.Attribute;
import com.s7175.kor.model.Component;
import com.s7175.kor.model.Parameter;
import com.s7175.kor.model.Server;

public class DB40Data {
	public static final String DATABASE_SOURCE = ("data/Databank.xml");
	public static final String DB_FILENAME = "KorData.db";
	private static DatabasePlaceholder oDatabank;
	
	private ObjectContainer dbConn;

	// should be generated via database mechanism...
	private static int _sID = 0;

	public static int sequenceServerID() {
		int newID = ++_sID;
		boolean freeID = false;
		while (!freeID) {
			for (Server s : getData().getServerList()) {
				if (s.getId() == newID) {
					freeID = false;
				}
			}
			freeID = true;
		}

		return newID;
	}

	public DB40Data() {
		initData();
		saveData();
	}

	private void initData() {
		oDatabank = DatabaseParser.parseDatabank(DATABASE_SOURCE);
	}

	public static DatabasePlaceholder getData() {
		return oDatabank;
	}

	public void saveData(boolean... save) {
		ExtObjectContainer con = getConnection().ext();
		Configuration config = con.configure();
		{
			ObjectClass objectClass = config.objectClass(Server.class);
			objectClass.objectField("name").indexed(true);
		}

		{
			ObjectClass objectClass = config.objectClass(Parameter.class);
			objectClass.objectField("name").indexed(true);
		}

		{
			ObjectClass objectClass = config.objectClass(Configuration.class);
			objectClass.objectField("name").indexed(true);
		}

		{
			ObjectClass objectClass = config.objectClass(Component.class);
			objectClass.objectField("name").indexed(true);
		}

		{
			ObjectClass objectClass = config.objectClass(Attribute.class);
			objectClass.objectField("name").indexed(true);
		}

		{
			ObjectClass objectClass = config
					.objectClass(ArchitectureView.class);
			objectClass.objectField("name").indexed(true);
		}

		ObjectSet<com.s7175.kor.model.Server> servers = con
				.query(com.s7175.kor.model.Server.class);
		if (servers.isEmpty()) {
			for (com.s7175.kor.model.Server s : oDatabank.getServerList()) {
				con.store(s);
			}
		}

		ObjectSet<com.s7175.kor.model.Parameter> parameters = con
				.query(com.s7175.kor.model.Parameter.class);
		if (parameters.isEmpty()) {
			for (com.s7175.kor.model.Parameter p : oDatabank
					.getParameterList()) {
				con.store(p);
			}
		}

		ObjectSet<com.s7175.kor.model.Configuration> citems = con
				.query(com.s7175.kor.model.Configuration.class);
		if (citems.isEmpty()) {
			for (com.s7175.kor.model.Configuration c : oDatabank
					.getConfigurationList()) {
				con.store(c);
			}
		}

		ObjectSet<com.s7175.kor.model.Component> compos = con
				.query(com.s7175.kor.model.Component.class);
		if (compos.isEmpty()) {
			for (com.s7175.kor.model.Component c : oDatabank
					.getComponentList()) {
				con.store(c);
			}
		}

		ObjectSet<com.s7175.kor.model.Attribute> attrs = con
				.query(com.s7175.kor.model.Attribute.class);
		if (attrs.isEmpty()) {
			for (com.s7175.kor.model.Attribute att : oDatabank
					.getAttributeList()) {
				con.store(att);
			}
		}

		ObjectSet<com.s7175.kor.model.ArchitectureView> archs = con
				.query(com.s7175.kor.model.ArchitectureView.class);
		if (archs.isEmpty()) {
			for (com.s7175.kor.model.ArchitectureView ar : oDatabank
					.getArchList()) {
				con.store(ar);
			}
		}

		con.commit();

		System.out.println("saving?");
		System.out.println(save != null && save.length > 0 && save[0]);
		if (save != null && save.length > 0 && save[0]) {
			System.out.println("true");
			DatabaseParser.saveDatabank(DATABASE_SOURCE, oDatabank);
		}
	}

	public ObjectContainer getConnection() {
		if (dbConn == null) {
			File dbFile = new File(DB_FILENAME);
			dbFile.delete();
			dbConn = Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(),
					DB_FILENAME);
		}
		return dbConn;
	}
}
